package com.example.javajetpacktest.base;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.example.javajetpacktest.network.HttpUtils;
import com.example.javajetpacktest.network.ITokenTimeout;
import com.example.javajetpacktest.network.LoginManager;
import com.example.javajetpacktest.ui.LoginActivity;


public abstract class BaseActivity<V extends ViewDataBinding> extends AppCompatActivity
        implements ITokenTimeout {

    private V binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, setLayout());
        binding.setLifecycleOwner(this);
        HttpUtils.getInstance().setTokenTimeoutListener(this);
        initViewModel();
        setBingData(binding);
        initData();
    }
    //初始化数据
    protected abstract void initData();
    //设置绑定的布局
    public abstract int setLayout();
    //为databing设置viewmodel
    public abstract void setBingData(V bingData);
    //初始化viewmodel
    public abstract void initViewModel();
    //Token超时
    @Override
    public void onTimeout() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        String name = this.getClass().getName();
        LoginManager.clearToken(this);
    }
}
